import java.io.*;
import java.net.*;

class MyServer implements Runnable {
    private static final int SOCKET = 11234;
    private static final String QUIT = "quit";
    private static int threadCount = 0;
    private Socket client;

    public MyServer(Socket c) {
        this.client = c;
    }

    public void run() {
        try {
            ++ threadCount;
            System.out.println("--- Welcome ---");
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            String command = "";
            do {
                command = reader.readLine();
                System.out.println(String.valueOf(threadCount) + " : " + command);
            } while (!QUIT.equals(command));
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            -- threadCount;
            System.out.println("--- See you, bye ---");
        }
    }

    public static void main(String args[]) {
        try {
            ServerSocket server = new ServerSocket(SOCKET);
            while (true) {
                Thread serverThread = new Thread(new MyServer(server.accept()));
                serverThread.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
